# -*- coding: utf-8 -*-

from dbobject import DBObject
from aeConn import dbHandle
import reaction

import yaml

class Compound(DBObject):
	"""This is a class that holds all the info that
		we keep about compounds in the database.
		Also it does automatic lookups for things when
		we don't have the info yet.
		PLEASE!!! Refer to things like this!!!!
		Things in this class:
		#pybel mol object
		self.mol

		#Object objects
		self.mol
		self.inchi

		#list of relation objects
		self.products
		self.reactants

		#fingerprint object
		self.fingerprint
	"""

	def __init__(self, key=''):
		"""Name is either the name of the compound or the id(int)
		"""
		if (type(key)==Compound):
			DBObject.__init__(self, key)
			return
		else:
			DBObject.__init__(self)

		self.key = key

		#dict of items we have (overriding __dict__)
		#mol object
		self._things['molfile'] = ""

		#string object
		self._things['name'] = ""
		self._things['formula'] = ""
		self._things['IUPACname'] = ""
		self._things['inchi'] = ""

		self._things['fingerprint'] = []

		self._reactions = {'reactant':[], 'product':[]}
		self._reactionsLoaded = False

	def __getattr__(self, name):

		if name=='reactions':
			if not self._reactionsLoaded:
				params = {'key':self.key}
				resp = dbHandle.getAction('getReactions', params)
				d = yaml.load(resp)
				self._reactions['reactant'] = [reaction.Reaction(key) for key in d['reactant']]
				self._reactions['product'] = [reaction.Reaction(key) for key in d['product']]

				self._reactionsLoaded = True

			return self._reactions
				

		else:
			return DBObject.__getattr__(self, name)


class EquCompound(Compound):

	def __init__(self, key='', percent=None):
		Compound.__init__(self, key)
		if percent != None:
			self._things['percent'] = percent
		else:
			self._things['percent'] = 0

	def __getattr__(self, name):

		if name=='percent':
			return self._things['percent']
		else:
			return Compound.__getattr__(self, name)
			
	def getType(self):
		return "Compound"
	
	def __del__(self):
		pass